package com.teamone.etl.datadictionary.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.teamone.etl.datadictionary.dto.UpdateDto;
import com.teamone.etl.datadictionary.entity.Dictionary;
import com.teamone.etl.datadictionary.service.DatadictionaryService;
import java.text.ParseException;
import java.util.List;
import javax.annotation.Resource;

import com.teamone.etl.exception.CommonCode;
import org.springframework.web.bind.annotation.*;

/**
 * @Description
 * @Author ZhaoPeiJie
 * @Date 2021/1/12 14:40
 * @Version 1.0
 */
@RequestMapping("/dictionary")
@RestController
public class DatadictionaryController {

    @Resource
    private DatadictionaryService datadictionaryService;

    @GetMapping("/findAll")
    public List<Dictionary> findAll(){
        return datadictionaryService.findAll();
    }

    @PostMapping("/conditionSearch")
    public List<Dictionary> conditionSearch(@RequestParam(value = "coding",required = false) Integer coding,
                                            @RequestParam(value = "dictionaryName",required = false) String dictionaryName,
                                            @RequestParam(value = "current",required = false) Long current,
                                            @RequestParam(value = "limit",required = false) Long limit){
        if(current==null){
            current=0L;
        }
        if(limit==null){
            limit=10L;
        }
        return datadictionaryService.findByDictionaryName(coding,dictionaryName,current,limit);
    }

    @PostMapping("/addParamDictionary")
    public CommonCode addParamDictionary(@RequestBody Dictionary dictionary) throws ParseException {
        int i = datadictionaryService.addParamDictionary(dictionary);
        if(i==1){
            return CommonCode.SUCCESS;
        }else {
            return CommonCode.FAIL;
        }
    }

    @GetMapping("/findByParentIdString")
    public List<String> findByParentIdString(){
        return datadictionaryService.findByParentIdString();
    }

    @PostMapping("/deleteDictionary/{id}")
    public R deleteDictionary(@PathVariable("id") String id){
        datadictionaryService.deleteDictionary(id);
        return R.ok(true);
    }

    @PostMapping("/findById")
    public Dictionary findById(@RequestParam("id") String id){
        Dictionary dictionary = datadictionaryService.findById(id);
        System.out.println(dictionary.toString());
        return datadictionaryService.findById(id);
    }

    @PostMapping("/updateDictionary")
    public CommonCode updateDictionary(@RequestBody UpdateDto updateDto){
        int i = datadictionaryService.updateDictionary(updateDto);
        if(i==1){
            return CommonCode.SUCCESS;
        }else {
            return CommonCode.FAIL;
        }
    }
}